package com.matrix.admin;

import org.mybatis.spring.annotation.MapperScan;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.scheduling.annotation.EnableAsync;

import java.net.*;
import java.util.Enumeration;

/**
 * <p> @EnableAsync 开启异步调用 </p>
 * <p> 并扫描com.matrix.admin包与com.matrix.common包及其子包下的包交给spring管理 </p>
 *
 * @author liuweizhong
 * @since 2024-02-05
 */
@EnableAsync
@SpringBootApplication(scanBasePackages = {"com.matrix.admin", "com.matrix.common"})
@MapperScan("com.matrix.admin.**.mappers")
@EnableAspectJAutoProxy(exposeProxy = true)
public class MatrixAdminApplication {

    private static final Logger logger = LoggerFactory.getLogger(MatrixAdminApplication.class);

    public static void main(String[] args) {
        ConfigurableApplicationContext context = SpringApplication.run(MatrixAdminApplication.class, args);
        printlnProjectInfo(context);
    }

    /**
     * 打印项目信息
     *
     * @param context 用于获取springboot配置
     */
    private static void printlnProjectInfo(ConfigurableApplicationContext context) {
        ConfigurableEnvironment environment = context.getEnvironment();
        String serverPort = environment.getProperty("server.port");
        String contextPath = environment.getProperty("server.servlet.context-path");
        String swaggerPath = environment.getProperty("springdoc.swagger-ui.path");
        String ip = "localhost";
        String ip6 = "localhost";
        try {
            ip = InetAddress.getLocalHost().getHostAddress();

            Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
            loop_while:
            while (interfaces.hasMoreElements()) {
                NetworkInterface networkInterface = interfaces.nextElement();
                // 可选：过滤出处于活动状态且不是回环的接口
                if (networkInterface.isUp() && !networkInterface.isLoopback()) {
                    Enumeration<InetAddress> addresses = networkInterface.getInetAddresses();
                    while (addresses.hasMoreElements()) {
                        InetAddress address = addresses.nextElement();
                        // 检查是否为IPv6地址
                        if (address instanceof Inet6Address && !address.getHostAddress().contains("%")) {
                            ip6 = address.getHostAddress();
                            break loop_while;
                        }
                    }
                }
            }
        } catch (SocketException | UnknownHostException e) {
            logger.error("获取本机IP地址失败");
        }
        // ipv6地址会有方括号，ipv4地址没有
        logger.info("""
                        --------<========= System startup successful! ========>---------
                        swagger路径:http://{}:{}{}{} 或者 http://[{}]:{}{}{}
                        knife4j文档路径:http://{}:{}{}/doc.html 或者 http://[{}]:{}{}/doc.html
                        """,
                ip, serverPort, contextPath, swaggerPath, ip6, serverPort, contextPath, swaggerPath,
                ip, serverPort, contextPath, ip6, serverPort, contextPath);
    }

}
